package com.redpack.quick.filter;

import com.redpack.quick.utils.OkHttpUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by shuttle on 4/9/18.
 */
@WebFilter(filterName = "authFilter", urlPatterns = "/*")
@Order(2)
public class AuthFilter implements Filter  {

    private static final Logger LOGGER = LoggerFactory.getLogger(AuthFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest)request;
        String authString = httpRequest.getHeader("auth");
        HttpSession session = httpRequest.getSession();
        LOGGER.info("authString:" + authString);
        LOGGER.info("session:" + session.getAttribute(authString).toString());
        if(StringUtils.isNotBlank(session.getAttribute(authString).toString())
                || ((HttpServletRequest) request).getRequestURL().toString().contains("/user/login")){
            LOGGER.info("验证:authString:" + session.getAttribute(authString).toString());
            chain.doFilter(request, response);
        }
        response.getWriter().write("-1");
    }

    @Override
    public void destroy() {

    }
}
